const mailer = require('nodemailer')
const logger = require('../lib/logger')('lib/mail')
const config = require('../config')
const mailConfig = config.mail

let transporter = mailer.createTransport(/*mail config*/)

function sendMail (data) {
  if (mailConfig.enable === false) {
    logger.log(data)
    return
  }

  transporter.sendMail(data, (err) => {
    if (err) logger.error(err)
  })
}

exports.sendMail = sendMail

exports.sendActiveMail = function (email, username, code) {
  let from = '' // mail config
  let to = email
  let subject = config.app.name = '账号激活'
  let html = [
    `<p>您好，${username}</p>`,
    `<p>我们收到您在${config.app.name}的注册信息，请您点击以下链接激活账号：</p>`,
    `<p><a href="${config.app.siteRoot}/c/user/active?code=${code}&username=${username}">激活链接</a></p>`,
    `<p>${config.app.name} 谨上。</p>`
  ].join('')

  sendMail({from, to, subject, html})
}
